﻿using System;
using System.Linq;
using System.Linq.Expressions;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.AboutGuild
{
	public partial class CouncilAdd : BasePage
	{
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)

				//else
				//    return ViewState["SystemType"].ToString();

				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemCode))
			{
				AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
				return;
			}
		}

		protected void SubBtn_Click(object sender, EventArgs e)
		{
			int CInfoIndex;
			Expression<Func<TB_CouncilInfo, bool>> fun = (p => true);
			Expression<Func<TB_CouncilInfo, int>> orderc = (p => p.Index);
			var resultCInfo = DAL.DALHelper.Instance.GetList<TB_CouncilInfo, int>(fun, orderc, true);

			if (resultCInfo.Count <= 0)
			{
				CInfoIndex = 1;
			}
			else
			{
				int Max = resultCInfo.Select(p => p.Index).Max();
				CInfoIndex = Max + 1;
			}

			TB_CouncilInfo NewObjC = DAL.DALHelper.Instance.Create<TB_CouncilInfo>();
			NewObjC.Index = CInfoIndex;
			NewObjC.Name = this.CouncilTitle.Text;
			if (string.IsNullOrWhiteSpace(this.StartDate.Text))
			{
				NewObjC.StartTime = Convert.ToDateTime( null );
			}
			else {
				NewObjC.StartTime = Convert.ToDateTime(this.StartDate.Text);
			}
			if (string.IsNullOrWhiteSpace(this.EndDate.Text))
			{
				NewObjC.EndTime = Convert.ToDateTime(null);
			}else{
				NewObjC.EndTime = Convert.ToDateTime(this.EndDate.Text);
			}
			
			NewObjC.Memo = this.Introduce.Text;
			NewObjC.CreateBy = MemberBasePage.UserInfo.UserName;
			NewObjC.CreateDate = DateTime.Now;
			NewObjC.EnName = this.ECouncilTitle.Text;
			NewObjC.EnMemo = this.EIntroduce.Text;
			DAL.DALHelper.Instance.Add<TB_CouncilInfo>(NewObjC);
			DAL.DALHelper.Instance.SaveChanges();


			Expression<Func<View_CinfoRelationMember, bool>> F = (p => p.Status);
			var resultCRM = DAL.DALHelper.Instance.GetList<View_CinfoRelationMember>(F);
			if (resultCRM.Count > 0)
			{
				foreach (var L in resultCRM)
				{
					TB_CouncilRelation NewObjCR = DAL.DALHelper.Instance.Create<TB_CouncilRelation>();
					NewObjCR.Index = CInfoIndex;
					NewObjCR.MemberID = L.MID;
					NewObjCR.Type = L.Type;
					DAL.DALHelper.Instance.Add<TB_CouncilRelation>(NewObjCR);
				}
			}
			DAL.DALHelper.Instance.SaveChanges();

			BasePage.CloseWindow(true);
			//ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script>window.opener.opener.Refresh();window.close();</script>");
			//return;
		}
	}
}